import { MemberTypeKeyMap } from './consts';

export const getSaveValue = (
  value: { [x: string]: any; key: string; id: string },
  multiTabs: boolean,
  _tabs: any,
  userDepartmentDisplayFormat: string,
  userUserGroupDisplayFormat: string,
) => {
  // 单个tab只保值存id
  // 多个tab 保存 type:id
  const [type] = value.key.split('-');
  let _extraInfo = '';
  if (MemberTypeKeyMap[type] == 0) {
    if (value?.usertype == 'userGroup') {
      if (userUserGroupDisplayFormat == 'positionName') {
        _extraInfo = (value?.positionid || value?.mainpositionid) ? `&4:${value?.positionid || value?.mainpositionid}` : '';
      } else {
        _extraInfo = (value?.groupid) ? `&3:${value?.groupid}` : '';
      };
    } else if (value?.usertype == 'department') {
      if (userDepartmentDisplayFormat == 'positionName') {
        _extraInfo = (value?.positionid || value?.mainpositionid) ? `&4:${value?.positionid || value?.mainpositionid}` : '';
      } else {
        _extraInfo = (value?.departmentid) ? `&5:${value?.departmentid}` : '';
      };
    };
  };
  return multiTabs
    ? `${MemberTypeKeyMap[type]}:${value.id}${_extraInfo}`
    : `${value.id}${_extraInfo}`;
};

export const getLowercase = (data: any[], tabKey?: string) => {
  return data.map((item) => {
    let _obj = {};
    for (const key in item) {
      _obj[key.toLowerCase()] = item[key];
    }
    if (tabKey) {
      _obj['key'] = `${tabKey}-${_obj?.['identityid']}`;
    }
    return _obj;
  });
};

export const nameFormat = (
  item: any,
  departmentDisplayFormat: string,
  positionDisplayFormat: string,
  userDepartmentDisplayFormat: string,
  userUserGroupDisplayFormat: string,
) => {
  if (item?.key?.startsWith('department')) {
    if (departmentDisplayFormat == 'departments') {
      return item?.deptpathname
        ? item?.deptpathname
          ?.split('/')
          ?.filter((item: any) => item)
          ?.join('/')
        : item?.name;
    } else {
      return item?.name;
    }
  } else if (item?.key?.startsWith('position')) {
    if (positionDisplayFormat == 'departmentPosition') {
      return item?.departmentname || item?.deptname
        ? (item?.departmentname || item?.deptname) + '/' + item?.name
        : item?.name;
    } else if (positionDisplayFormat == 'departmentsPosition') {
      return item?.deptpathname
        ? item?.deptpathname
          ?.split('/')
          ?.filter((item: any) => item)
          ?.join('/') + (item?.deptpathname?.indexOf(item?.name) == -1 ? '/' + item?.name : '')
        : item?.name;
    } else {
      return item?.name;
    }
  } else if (item?.key?.startsWith('user')) {
    if (item?.usertype == 'userGroup') {
      if (userUserGroupDisplayFormat == 'nameUsername') {
        return (item?.userloginname || item?.account)
          ? item?.name + '(' + (item?.userloginname || item?.account) + ')'
          : item?.name;
      } else if (userUserGroupDisplayFormat == 'userGroupName') {
        return (item?.groupname || item?.usergroup) ? (item?.groupname || item?.usergroup) + '/' + item?.name : item?.name;
      } else if (userUserGroupDisplayFormat == 'positionName') {
        return (item?.selectedposition || item?.mainpositionname) ? (item?.selectedposition || item?.mainpositionname) + '/' + item?.name : item?.name;
      } else {
        return item?.name;
      }
    } else if (item?.usertype == 'department') {
      if (userDepartmentDisplayFormat == 'nameUsername') {
        return (item?.userloginname || item?.account)
          ? item?.name + '(' + (item?.userloginname || item?.account) + ')'
          : item?.name;
      } else if (userDepartmentDisplayFormat == 'departmentName') {
        return item?.departmentname || item?.deptname ? (item?.departmentname || item?.deptname) + '/' + item?.name : item?.name;
      } else if (userDepartmentDisplayFormat == 'positionName') {
        return (item?.selectedposition || item?.mainpositionname) ? (item?.selectedposition || item?.mainpositionname) + '/' + item?.name : item?.name;
      } else {
        return item?.name;
      }
    } else {
      return (item?.selectedposition || item?.mainpositionname) ? (item?.selectedposition || item?.mainpositionname) + '/' + item?.name : item?.name;
    }
  } else {
    return item?.name;
  }
};
